/**
 * 鼠标弹框提示层
 *
 * @class PersonHoverbox
 * @extends AbstractHoverbox
 * @constructor
 * @param {Person} personNode 
 * @param {Number} centerX 
 * @param {Number} centerY 
 * @param {Raphael.st} nodeShapes 
 */

var PersonHoverbox = Class.create(AbstractHoverbox, {

    initialize: function($super, personNode, centerX, centerY, nodeShapes) {
        var radius = PedigreeEditor.attributes.personHoverBoxRadius;
        $super(personNode, -radius, -radius, radius * 2, radius * 2, centerX, centerY, nodeShapes);
    },

    /**
     *
     * @method generateHandles
     * @return {Raphael.st} 
     */
    generateHandles: function($super) {
        var hiddenFlag = parent.$("#hiddenFlag").val();
		if(hiddenFlag == "true"){
            return;
        }
        if (this._currentHandles !== null) return;
        $super();
        var x          = this.getNodeX();
        var y          = this.getNodeY();
        var node       = this.getNode();
        var nodeShapes = node.getGraphics().getGenderGraphics().flatten();

        editor.getPaper().setStart();

        if (PedigreeEditor.attributes.newHandles) {
            var strokeWidth = editor.getWorkspace().getSizeNormalizedToDefaultZoom(PedigreeEditor.attributes.handleStrokeWidth);

            var partnerGender = 'U';
            if (node.getGender() == 'F') partnerGender = 'M';
            if (node.getGender() == 'M') partnerGender = 'F';

            var splitLocationY = y-PedigreeEditor.attributes.personHandleBreakY+18;
            var splitLocationX =x-PedigreeEditor.attributes.personSiblingHandleLengthX-strokeWidth/3;
            var siblingOrbX = x-PedigreeEditor.attributes.personSiblingHandleLengthX-strokeWidth/2 - 20;
            var siblingOrbY = splitLocationY+PedigreeEditor.attributes.personSiblingHandleLengthY +5;
            this.generateHandle('sibling', splitLocationX, splitLocationY, siblingOrbX ,siblingOrbY ,
                                "添加兄妹", "U");
            if (editor.getGraph().getParentRelationship(node.getID()) === null) {
                var topHandleHint = undefined;
                if (PedigreeEditor.attributes.enableHandleHintImages) {
                    var hintSize = PedigreeEditor.attributes.radius/2;
                    var path = [["M", x-hintSize, y- PedigreeEditor.attributes.personHandleLength],["L", x+hintSize, y- PedigreeEditor.attributes.personHandleLength]];
                    var line1  = editor.getPaper().path(path).attr({"stroke-width": strokeWidth/3, stroke: "#555555"}).toBack();
                    var father = editor.getPaper().rect(x-hintSize-11,y-PedigreeEditor.attributes.personHandleLength-5.5,11,11).attr({fill: "#CCCCCC"}).toBack();
                    var mother = editor.getPaper().circle(x+hintSize+6,y-PedigreeEditor.attributes.personHandleLength,6).attr({fill: "#CCCCCC"}).toBack();
                    var topHandleHint = editor.getPaper().set().push(line1, father, mother);
                }
                this.generateHandle('parent', x-7, splitLocationY, x-7, y - PedigreeEditor.attributes.personHandleLength - 10,
                                    "添加父母。", "F", topHandleHint);
            }
            else {
                if (PedigreeEditor.attributes.enableHandleHintImages) {
                    var path = [["M", x, splitLocationY],["L", x, y-PedigreeEditor.attributes.personHoverBoxRadius+4]];
                    editor.getPaper().path(path).attr({"stroke-width": strokeWidth, stroke: "gray"}).insertBefore(nodeShapes);
                }
            }

            if (!node.isFetus()) {
                if (node.getChildlessStatus() === null) {
                }

                var vertPosForPartnerHandles = y;
                this.generateHandle('partnerR', x + PedigreeEditor.attributes.personHandleBreakX - 2, vertPosForPartnerHandles, x + PedigreeEditor.attributes.personHandleLength, vertPosForPartnerHandles,
                                    "添加配偶", partnerGender);
            }
        }
        else {
            if (editor.getGraph().getParentRelationship(node.getID()) === null)
                this.generateHandle('parent',   x, y, x, y - PedigreeEditor.attributes.personHandleLength, "添加父亲2");

            if (!node.isFetus()) {
                if (node.getChildlessStatus() === null)
//              generateHandle: function(type, startX, startY, orbX, orbY, title, orbShapeGender, toHide)
                this.generateHandle('child',x, y, x, y + PedigreeEditor.attributes.personHandleLength, "添加子女r");
                this.generateHandle('partnerR', x, y, x + PedigreeEditor.attributes.personHandleLength, y, "添加配偶R");
                this.generateHandle('partnerL', x, y, x - PedigreeEditor.attributes.personHandleLength, y, "添加配偶L");
            }
        }

        this._currentHandles.push( editor.getPaper().setFinish() );
    },

    /**
     *
     * @method generateButtons
     */
    generateButtons: function($super) {
        var hiddenFlag = parent.$("#hiddenFlag").val();
		if(hiddenFlag == "true"){
            return;
        }
        if (this._currentButtons !== null) return;
        $super();
        this.generateMenuBtn();
        if (!this.getNode().isProband())
            this.generateDeleteBtn();
    },

    /**
     * @method generateMenuBtn
     * @return {Raphael.st} The generated button
     */
    generateMenuBtn: function() {
        var me = this;
        var action = function() {
            me.toggleMenu(!me.isMenuToggled());
        };
        var genderShapedButton = this.getNode().getGraphics().getGenderShape().clone();
        genderShapedButton.attr(PedigreeEditor.attributes.nodeShapeMenuOff);
        genderShapedButton.click(action);
        genderShapedButton.hover(function() { genderShapedButton.attr(PedigreeEditor.attributes.nodeShapeMenuOn)},
                                 function() { genderShapedButton.attr(PedigreeEditor.attributes.nodeShapeMenuOff)});
        genderShapedButton.attr("cursor", "pointer");
        this._currentButtons.push(genderShapedButton);
        this.disable();
        this.getFrontElements().push(genderShapedButton);
        this.enable();
    },

    /**
     *
     * @method isMenuToggled
     * @return {Boolean}
     */
    isMenuToggled: function() {
        return this._isMenuToggled;
    },

    /**
     *
     * @method toggleMenu
     */
    toggleMenu: function(isMenuToggled) {
        if (this._justClosedMenu) return;
        this._isMenuToggled = isMenuToggled;
        if(isMenuToggled) {
            this.getNode().getGraphics().unmark();
            var optBBox = this.getBoxOnHover().getBBox();
            var x = optBBox.x2;
            var y = optBBox.y;
            var position = editor.getWorkspace().canvasToDiv(x+5, y);
            sessionStorage.setItem("_current_personid", this.getNode().getID());
            editor.getNodeMenu().show(this.getNode(), position.x, position.y);
        }
    },

    /**
     *
     * @method animateHideHoverZone
     */
    animateHideHoverZone: function($super) {
        var hiddenFlag = parent.$("#hiddenFlag").val();
		if(hiddenFlag == "true"){
            return;
        }
        this._hidden = true;
        if(!this.isMenuToggled()){
            var parentPartnershipNode = editor.getGraph().getParentRelationship(this.getNode().getID());
            if (parentPartnershipNode && editor.getNode(parentPartnershipNode))
                editor.getNode(parentPartnershipNode).getGraphics().unmarkPregnancy();
            $super();
        }
    },

    /**
     *
     * @method animateDrawHoverZone
     */
    animateDrawHoverZone: function($super) {
        var hiddenFlag = parent.$("#hiddenFlag").val();
		if(hiddenFlag == "true"){
            return;
        }
        this._hidden = false;
        if(!this.isMenuToggled()){
            var parentPartnershipNode = editor.getGraph().getParentRelationship(this.getNode().getID());
            if (parentPartnershipNode && editor.getNode(parentPartnershipNode))
                editor.getNode(parentPartnershipNode).getGraphics().markPregnancy();
            $super();
        }
    },

    /**
     *
     * @method handleAction
     * @param {String} handleType "child", "partner" or "parent"
     * @param {Boolean} isDrag 
     */
    handleAction : function(handleType, isDrag, curHoveredId) {
        console.log("handleType: " + handleType + ", isDrag: " + isDrag + ", curHovered: " + curHoveredId);

        if(isDrag && curHoveredId !== null) {

            if(handleType == "parent") {
                this.removeHandles();
                this.removeButtons();
                var event = { "personID": this.getNode().getID(), "parentID": curHoveredId };
                document.fire("pedigree:person:drag:newparent", event);
            }
            else if(handleType == "partnerR" || handleType == "partnerL") {
                this.removeHandles();
                var event = { "personID": this.getNode().getID(), "partnerID": curHoveredId };
                document.fire("pedigree:person:drag:newpartner", event);
            }
            else if(handleType == "child") {
                var event = { "personID": curHoveredId, "parentID": this.getNode().getID() };
                document.fire("pedigree:person:drag:newparent", event);
            }
            else if(handleType == "sibling") {
                var event = { "sibling2ID": curHoveredId, "sibling1ID": this.getNode().getID() };
                document.fire("pedigree:person:drag:newsibling", event);
            } else if(handleType == "removeTheNode") {
            		if (!this.getNode().isProband())
            		this.generateDeleteBtn();
            }
        }
        else if (!isDrag) {
            if(handleType == "partnerR" || handleType == "partnerL") {
                this.removeHandles();
                var preferLeft = (this.getNode().getGender() == 'F') || (handleType == "partnerL");
                var event = { "personID": this.getNode().getID(), "preferLeft": preferLeft };
                document.fire("pedigree:person:newpartnerandchild", event);
            }
            else if(handleType == "child") {
                var position = editor.getWorkspace().canvasToDiv(this.getNodeX(), (this.getNodeY() + PedigreeEditor.attributes.personHandleLength + 15));
                editor.getNodetypeSelectionBubble().show(this.getNode(), position.x, position.y);
            }
            else if(handleType == "sibling") {
                var position = editor.getWorkspace().canvasToDiv(this.getNodeX() - PedigreeEditor.attributes.personSiblingHandleLengthX,
                this.getNodeY() - PedigreeEditor.attributes.personHandleBreakY+PedigreeEditor.attributes.personSiblingHandleLengthY + 15);
                editor.getSiblingSelectionBubble().show(this.getNode(), position.x, position.y);
            }
            else if(handleType == "parent") {
                this.removeHandles();
                this.removeButtons();
                var event = { "personID": this.getNode().getID() };
                document.fire("pedigree:person:newparent", event);
            }
            else if(handleType == "removeTheNode") {
            		if (!this.getNode().isProband())
            		this.generateDeleteBtn();
            }
        }
        this.animateHideHoverZone();
    }
});
